package com.ykdl.member.kid.fragments;

import java.util.ArrayList;
import java.util.List;

import com.lidroid.xutils.BitmapUtils;
import com.ykdl.member.kid.R;
import com.ykdl.member.constant.KidAction;
import com.ykdl.member.kid.adapters.DoctorTalkChildListAdapter;
import com.ykdl.member.kid.adapters.TopicSheetListAdapter;
import com.ykdl.member.kid.circle.TopicAndResultActivity;
import com.ykdl.member.kid.doctor.AskDoctorActivity;
import com.ykdl.member.kid.doctor.CatalogsDoctorListActivity;
import com.ykdl.member.kid.doctor.ProblemDetailsAcivity;
import com.ykdl.member.kid.gears.ArticleWapActivity;
import com.ykdl.member.kid.gears.DoctorTalkChildListActivity;
import com.ykdl.member.kid.gears.HomeActivity;
import com.ykdl.member.kid.gears.TopicsSheetListActivity;
import com.ykdl.member.kid.gears.WebViewActivity;
import com.ykdl.member.kid.image.ImageOptionsFactory;
import com.ykdl.member.kid.messagebox.MyMessageAcivity1;
import com.ykdl.member.kid.models.ArticleBean;
import com.ykdl.member.kid.models.CatalogArticleItem;
import com.ykdl.member.kid.models.CatalogBean;
import com.ykdl.member.kid.models.CatalogItemBean;
import com.ykdl.member.kid.models.CatalogTopicItem;
import com.ykdl.member.kid.models.FileMetas;
import com.ykdl.member.kid.models.HomeBean;
import com.ykdl.member.kid.models.PostActor;
import com.ykdl.member.kid.models.TopicBean;
import com.ykdl.member.kid.util.MobileUtils;
import com.ykdl.member.kid.widget.AsyncImageView;
import com.ykdl.member.views.MyGridView;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

public class DoctorTalkChildFragment extends Fragment {
	private HomeActivity  activity;
	private MyGridView gv_doctor_talk_child;
	private ArrayList<Object> objs;
	
	private int gradview_width;
	private int screenWidth;
	private int gradview_item_width;
	private int gridview_item_image_height;
	private RelativeLayout rl_home_doctor_talk_child_title;
	private BitmapUtils bitmapUtil = null;
	
	private DoctorTalkChildListAdapter adapter;
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		this.activity = (HomeActivity)activity;
		bitmapUtil = new BitmapUtils(this.activity);
		
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View  view = LayoutInflater.from(activity).inflate(R.layout.doctor_talk_child_view, null);
		rl_home_doctor_talk_child_title = (RelativeLayout) view.findViewById(R.id.rl_home_doctor_talk_child_title);
		gv_doctor_talk_child = (MyGridView) view.findViewById(R.id.gv_doctor_talk_child);
//		gv_doctor_talk_child.setSelector(new ColorDrawable(Color.TRANSPARENT));
		return view;
		
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Bundle  bundle = this.getArguments();
//		topics = (List<CatalogBean>) bundle.getSerializable("topics");
		 final  HomeBean  homeBean = (HomeBean) bundle.getSerializable("homebean");
		 objs = (ArrayList<Object>) homeBean.getObjs();
		 
		/**设置适配**/
		 screenWidth = MobileUtils.getScreenWidthIntPx();
		 gradview_width = (screenWidth - MobileUtils.dpToPx(12)); //gridview的宽度
		 gradview_item_width = gradview_width / 2; // gridview 每项item的宽度
		 gridview_item_image_height = (int)((gradview_item_width  - MobileUtils.dpToPx(12)) / 1.5); //gridview 每项item的图片高度
		
		LayoutParams  params = (LayoutParams) gv_doctor_talk_child.getLayoutParams();
		params.width = gradview_width;
		params.rightMargin = MobileUtils.dpToPx(6);
		params.leftMargin = MobileUtils.dpToPx(6);
		
		gv_doctor_talk_child.setHorizontalSpacing(MobileUtils.dpToPx(12));
		gv_doctor_talk_child.setVerticalSpacing(MobileUtils.dpToPx(12));
		gv_doctor_talk_child.setColumnWidth(gradview_item_width);
		gv_doctor_talk_child.setCacheColorHint(getResources().getColor(R.color.touming));
		gv_doctor_talk_child.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
		gv_doctor_talk_child.setNumColumns(2);
		
		//设置适配器
		adapter = new DoctorTalkChildListAdapter(this.activity.getApplicationContext(), bitmapUtil, gridview_item_image_height);
		adapter.setList(objs);
		gv_doctor_talk_child.setAdapter(adapter);
		
		gv_doctor_talk_child.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int position,
					long arg3) {
				final Object  obj = objs.get(position);
				Intent  intent = null;
				if(obj instanceof ArticleBean){//文章 , webview 加载文章
					intent = new Intent(activity, ArticleWapActivity.class);
					intent.putExtra("article_id", ((ArticleBean)obj).getArticle_id()+"");
					
				}else if(obj instanceof CatalogBean){ // 问题 （分为一个问题 和 多个问题）
					final CatalogBean catalog = (CatalogBean)obj;
//					if(catalog.getCatalog_type() == 100){ //普通群组专题
//						if(catalog.getCatalogItems().size() > 1){ //多个专题
//							intent = new Intent(activity, TopicsSheetListActivity.class);
//							intent.putExtra("catalog_id", catalog.getCatalog_id() + "");
//						}else{//一个专题 (进入专题详情)
//							if(catalog.getItems() ==null || "[]".equals(catalog.getItems()) || catalog.getItems().size() == 0)return;
//							CatalogTopicItem  topic = (CatalogTopicItem) catalog.getItems().get(0);
//							if(topic == null || topic.getItem() == null)return;
//							
//							intent = new Intent(activity, TopicAndResultActivity.class);
//							intent.putExtra(TopicAndResultActivity.STR_TOPIC_ID, topic.getItem().getTopic_id()+ "");
//							intent.putExtra(TopicAndResultActivity.STR_GROUP_ID, topic.getItem().getSource_id() + "");
//						}
//					}else if(catalog.getCatalog_type() == 300){
//						if(catalog.getCatalogItems().size() > 1){ 
//							intent = new Intent(activity, CatalogsDoctorListActivity.class);
//							intent.putExtra(KidAction.DOCTOR_CATALOG_ID, catalog.getCatalog_id() + "");
//						}else{
//							if(catalog.getItems() ==null || "[]".equals(catalog.getItems())|| catalog.getItems().size() == 0)return;
//							final CatalogTopicItem  topic = (CatalogTopicItem) catalog.getItems().get(0);
//							if(topic == null || topic.getItem() == null)return;
//							intent = new Intent(activity,ProblemDetailsAcivity.class);
//							intent.putExtra(KidAction.DOCTOR_TOPICI_DD, topic.getItem().getTopic_id()+"");
//						}
//					}else if(catalog.getCatalog_type() == 2){ //问医生
//						if(catalog.getCatalogItems().size() > 1){ //多个问医生专题
//							intent = new Intent(activity, TopicsSheetListActivity.class);
//							intent.putExtra("catalog_id", catalog.getCatalog_id() + "");
//						}else{//一个问医生专题 (进入专题详情wap)
//							if(catalog.getItems() ==null || "[]".equals(catalog.getItems())|| catalog.getItems().size() == 0)return;
//							if(!(catalog.getItems().get(0) instanceof CatalogArticleItem))return;
//							CatalogArticleItem   topic= (CatalogArticleItem) catalog.getItems().get(0);
//							if(topic == null || topic.getItem() == null)return;
//							intent = new Intent(activity, ArticleWapActivity.class);
//							intent.putExtra("article_id", topic.getItem().getArticle_id()+"");
//						}
						if(catalog.getCatalogItems().size() > 1){ 
//							intent = new Intent(activity, CatalogsDoctorListActivity.class);
//							intent.putExtra(KidAction.DOCTOR_CATALOG_ID, catalog.getCatalog_id() + "");
							intent =new Intent(activity,TopicsSheetListActivity.class);
							intent.putExtra("catalog_id", catalog.getCatalog_id() + "");
						}else{
							if(catalog.getCatalogItems() ==null || "[]".equals(catalog.getCatalogItems())|| catalog.getCatalogItems().size() == 0)return;
//							if(!(catalog.getCatalogItems().get(0) instanceof CatalogTopicItem))return;
							final CatalogTopicItem  topic = (CatalogTopicItem) catalog.getCatalogItems().get(0);
							if(topic == null || topic.getItem() == null)return;
							intent = new Intent(activity,ProblemDetailsAcivity.class);
							intent.putExtra(KidAction.DOCTOR_TOPICI_DD, topic.getItem().getTopic_id()+"");
						}
					//}
					
				}
				
				activity.startActivity(intent);
			}
			
		});
		
		rl_home_doctor_talk_child_title.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent t = new Intent(activity,  DoctorTalkChildListActivity.class);
				startActivity(t);
			}
		});
		
	}
}
